package com.newbingo.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IAgentDao;
import com.newbingo.entity.Agent;
import com.newbingo.entity.GameTable;
import com.newbingo.util.Pager;

public class AgentDao extends HibernateDaoSupport implements IAgentDao {
	private static final Log log = LogFactory.getLog(AgentDao.class);

	/*
	 * 根据用户名和密码进行登录验证
	 */
	public Agent findByLogin(String uname, String pwd) {
		log.debug("finding Agent instance by login");
		try {
			Agent agent = null;
			String hql = "from Agent as agent where agent.uname=? and upwd=?";
			Query query = getSession().createQuery(hql);
			query.setParameter(0, uname);
			query.setParameter(1, pwd);
			List<Agent> agents = query.list();
			if (agents != null && agents.size() > 0) {
				agent = agents.get(0);
			}
			return agent;
		} catch (RuntimeException re) {
			log.error("finding failed");
			throw re;
		}
	}

	public void attachDirty(Agent instance) {
		log.debug("attaching dirty Agent instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attached failed", re);
			throw re;
		}
	}

	public void delete(Agent transientInstance) {
		log.debug("deleting Agent transientInstance");
		try {
			getSession().delete(transientInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * 根据代理商ID查询出代理商设置的投注格设置
	 */
	public Pager findBetSettingByAgentId(int start, int limit, int agentid) {
		Pager pager = new Pager();
		Criteria cri = this.getSession().createCriteria(Agent.class);
		cri.add(Expression.eq("uno", agentid));
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		List list = cri.list();
		pager.setData(list);
		return pager;
	}

	/*
	 * 根据代理的ID和下属代理(股东)的类型agentType查询代理或股东
	 */
	public Pager findSubAgents(int start, int limit, Integer agentId,
			Integer agentType) {
		log.debug("getting sub agents by agentID ande agentType");
		try {
			Pager page = new Pager();
			Criteria cri = getSession().createCriteria(Agent.class);
			if (agentId != null) {
				cri.createAlias("parentAgent", "parentAgent");
				cri.add(Expression.eq("parentAgent.uno", agentId));
			}
			cri.add(Expression.eq("agentType", agentType));
			cri.setProjection(Projections.rowCount());
			page.setTotalSize(((Number) cri.uniqueResult()).intValue());
			cri.setProjection(null);
			cri.setFirstResult(start);
			cri.setMaxResults(limit);
			cri.setResultTransformer(Criteria.ROOT_ENTITY);
			page.setData(cri.list());
			return page;
		} catch (RuntimeException re) {
			log.error("getting failed", re);
			throw re;
		}
	}

	public Pager findByPage(int start, int limit) {
		Pager pager = new Pager();
		Criteria cri = this.getSession().createCriteria(Agent.class);
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		List list = cri.list();
		pager.setData(list);
		System.out.println("Pager的大小：" + pager.getTotalSize());
		return pager;
	}

	public List findAll() {
		log.debug("finding All Agent instance");
		try {
			String queryString = "from Agent";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find failed", re);
			throw re;
		}
	}

	public Agent findById(Integer id) {
		log.debug("getting Agent instance with id:" + id);
		try {
			Agent instance = (Agent) getSession().get(
					"com.newbingo.entity.Agent", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("getting failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return this.findByProperty(NAME, name);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Agent instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Agent as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByStatus(Object status) {
		return this.findByProperty(STATUS, status);
	}

	public Agent merge(Agent Instance) {
		log.debug("merging Agent Instance");
		try {
			Agent result = (Agent) getSession().merge(Instance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void save(Agent Instance) {
		log.debug("saving Agent Instance");
		try {
			getSession().save(Instance);
			log.debug("save Agent successful");
		} catch (RuntimeException re) {
			log.error("save Agent failed", re);
			throw re;
		}
	}

	public List findByExample(Agent instance) {
		log.debug("finding Agent instance by example");
		try {
			List results = getSession().createCriteria(
					"com.newbingo.entity.Agent").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

}
